package org.tis.tools.senior.module.developer.controller;

import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.senior.module.developer.entity.SRollback;
import org.tis.tools.senior.module.developer.entity.vo.RollbackDetail;
import org.tis.tools.senior.module.developer.service.ISRollbackService;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * sRollback的Controller类
 * 
 * @author Auto Generate Tools
 * @date 2018/09/26
 */
@RestController
@RequestMapping("/sRollback")
public class SRollbackController extends BaseController<SRollback>  {

    @Autowired
    private ISRollbackService sRollbackService;

    @PostMapping("/add")
    public ResultVO add(@RequestBody @Validated SRollback sRollback) {
        sRollbackService.insert(sRollback);
        return ResultVO.success("新增成功！");
    }
    
    @PutMapping
    public ResultVO update(@RequestBody @Validated SRollback sRollback) {
        sRollbackService.updateById(sRollback);
        return ResultVO.success("修改成功！");
    }

    /**
     * 删除回退申请
     * @param guid
     * @return
     */
    @ApiOperation(value = "删除回退申请", notes = "url传参")
    @DeleteMapping("/{guid}")
    public ResultVO delete(@PathVariable
                               @ApiParam(name = "guid", value = "回退申请guid", required = true)
                               @NotNull(message = "guid不能为空") Integer guid) {

        sRollbackService.deleteRollbackBuGuid(guid);
        return ResultVO.success("删除成功");
    }
    
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") String id) {
        SRollback sRollback = sRollbackService.selectById(id);
        if (sRollbackService == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", sRollback);
    }

    /**
     * 确认回退申请代码
     * @param guidRollback
     * @return
     */
    @ApiOperation(value = "生成回退申请", notes = "url传参")
    @GetMapping("/{guidRollback}/affirmRollback")
    public ResultVO affirmRollback(@PathVariable
                                   @ApiParam(name = "guid", value = "回退申请guid", required = true)
                                   @NotNull(message = "回退申请guid不能为空")
                                           Integer guidRollback){
        sRollbackService.affirmRollbackSeccess(guidRollback);
        return ResultVO.success("回退成功");
    }

    /**
     * 查询所有回退申请
     * @return
     */
    @ApiOperation(value = "查询所有回退申请")
    @PostMapping("/list")
    public ResultVO list(@RequestBody
                             @ApiParam(name = "page", value = "分页对象", required = true)
                             @Validated
                                     SmartPage<RollbackDetail> page){

        Page<RollbackDetail> workitemDetailPage = new Page<>
                (page.getPage().getCurrent(), page.getPage().getSize(),
                        page.getPage().getOrderByField(), page.getPage().getAsc());

        return ResultVO.success("查询成功", sRollbackService.selectRollbackList(
                    workitemDetailPage,getWrapper(page.getCondition())));
    }


    
}

